<?php
	
	namespace Aspic\Image;
	use Aspic as A;

	abstract class AbstractImage {
		protected $_img;
		protected $_width;
		protected $_height;
		
		protected $_path;
		
		public function __construct($path = null) {
			$this->_img = null;
			$this->_path = $path;
			
			if($path) {
				if(!is_file($path)) {
					throw new Exception('File not found: "'.$path.'"');
				}
				else {
					list($width, $height, $type, $attr) = getimagesize($path);
					
					$this->_width = $width;
					$this->_height = $height;
				}
			}
			else {
				$this->_width = null;
				$this->_height = null;
			}
		}
		
		public function getResource() {
			return $this->_img;
		}
		
		public function setResource($res) {
			$this->_img = $res;
			$this->_width = imagesx($res);
			$this->_height = imagesy($res);
		}
		
		/**
		* Save the image to $path (or registered path if null)
		*/
		abstract public function save($path = null);
		
		/**
		* Return a number that match the quality given proportional to the range of quality of image type (jpeg [0-100],  png [9-0])
		*/
		public function percentQuality($pc) {
			throw new A\NotImplementedException;
		}
		
		public function resize($width, $height) {
			$newImage = imagecreatetruecolor($width, $height);
			
			$res = imagecopyresampled(
				$newImage, 
				$this->_img, 
				0, 
				0, 
				0, 
				0, 
				$width, 
				$height, 
				$this->_width, 
				$this->_height
			);
			
			if(!$res) {
				throw new Exception('Resize failed');
			}
			else {
				return static::createFromRessource($newImage);
			}
		}
		
		/**
		* Resize image if it is bigger than specified width/height
		*/
		public function resizeMax($maxWidth, $maxHeight) {
			$widthGap = null;
			$heightGap = null;
			$newWidth = $this->_width;
			$newHeight = $this->_height;
			
			if($newWidth > $maxWidth) {
				$widthGap = $this->_width - $maxWidth;
			}
			
			if(is_int($widthGap)) {
				$pcent = $maxWidth / $newWidth;
				$newWidth = $maxWidth;
				$newHeight = $newHeight * $pcent;
			}
			
			if($newHeight > $maxHeight) {
				$heightGap = $this->_height - $maxHeight;
			}
			
			if(is_int($heightGap)) {
				$pcent = $maxHeight / $newHeight;
				$newHeight = $maxHeight;
				$newWidth = $newWidth * $pcent;
			}
			
			settype($newWidth, 'int');
			settype($newHeight, 'int');
			
			$newImage = imagecreatetruecolor($newWidth, $newHeight);
			
			$res = imagecopyresampled(
				$newImage, 
				$this->_img, 
				0, 
				0, 
				0, 
				0, 
				$newWidth, 
				$newHeight, 
				$this->_width, 
				$this->_height
			);
			
			if(!$res) {
				throw new A\Exception('Resize max failed');
			}
			else {
				$this->_img = $newImage;
				$this->_width = $newWidth;
				$this->_height = $newHeight;
//				return static::createFromResource($newImage, $this->_path);
			}
		}
		
		public function __clone() {
			$original = $this->_img;
			$copy = imagecreatetruecolor($this->_width, $this->_height);
			
			imagecopy($copy, $original, 0, 0, 0, 0, $this->_width, $this->_height);
			
			$this->_img = $copy;
		}
		
		public static function createFromResource($ress, $path = null) {
			$currentClass = get_called_class();
			
			$img = new $currentClass($path);
			
			$img->setResource($ress);
			
			return $img;
		}
		
		public static function open($path) {
			$ext = strtolower(A\UString::getFileExtension($path));
				
			if($ext == 'jpg' Or $ext == 'jpeg') {
				$img = new JPEG($path);
			}
			elseif($ext == 'png') {
				$img = new PNG($path);
			}
			elseif($ext == 'gif') {
				$img = new GIF($path);
			}
			else {
				throw new A\Exception('Extension "'.$ext.'" is not recognized'.$path);
			}
			
			return $img;
		}
		
		public static function throwImageCreationError() {
			throw new Exception('Error occured while creating image in memory');
		}
		
	}
	
?>